package org.csu.xzypetstore.service;


import org.csu.xzypetstore.controller.auto;
import org.csu.xzypetstore.domain.Category;
import org.csu.xzypetstore.domain.Item;
import org.csu.xzypetstore.domain.Product;
import org.csu.xzypetstore.persistence.CategoryMapper;
import org.csu.xzypetstore.persistence.ItemMapper;
import org.csu.xzypetstore.persistence.ProductMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@MapperScan("org.csu.xzypetstore.persistence")
public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItemByProductId(String productId)
    {
        return itemMapper.getItem(productId);
    }
    public Product getProduct(String productId)
    {
        return productMapper.getProduct(productId);
    }
    public Item getItem(String itemId)
    {
        return itemMapper.GetItem(itemId);
    }
    public boolean isItemInStock(String itemId){
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }

    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    //新增category
    public void addCategory(Category category) {
        categoryMapper.addCategory(category);
    }

    //删除category
    public void delCategory(String categoryId) {
        categoryMapper.delCategory(categoryId);
        List<Product> list1 = productMapper.getProductListByCategory(categoryId);
        for(Product l1:list1){
            productMapper.delProduct(l1.getProductId());
            List<Item> list2 = itemMapper.getItemListByProduct(l1.getProductId());
            for(Item l2:list2){
                itemMapper.delItem(l2.getItemId());
            }
        }
    }

    //更新category
    public void updCategory(Category category){
        categoryMapper.updCategory(category);
    }

    //新增product
    public void addProduct(Product product){
        productMapper.addProduct(product);
    }

    //删除product
    public void delProduct(String productId){
        productMapper.delProduct(productId);
        List<Item> list1 = itemMapper.getItemListByProduct(productId);
        for(Item l1:list1){
            itemMapper.delItem(l1.getItemId());
        }
    }

    //更新product
    public void updProduct(Product product){
        productMapper.updProduct(product);
    }

    //新增item
    public void addItem(Item item){
        itemMapper.addItem(item);
    }

    //删除item
    public void delItem(String itemId){
        itemMapper.delItem(itemId);
    }

    //更新item
    public void updItem(Item item){
        itemMapper.updItem(item);
    }

}
